package com.lzw.security.service.impl;

import com.alibaba.fastjson.JSON;
import com.lzw.security.entity.*;
import com.lzw.security.exception.TidcNulException;
import com.lzw.security.mapper.*;
import com.lzw.security.ov.Status;
import com.lzw.security.ov.UserOV;
import com.lzw.security.service.CheckService;
import com.lzw.security.util.RedisUtil;
import com.lzw.security.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.core.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.ServiceMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020-3-28 20:50
 * @description：
 * @modified By：
 * @version:
 */
@Slf4j
@Service
public class CheckServiceImpl implements CheckService {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private SeckillMapper seckillMapper;
    @Autowired
    private DosingMapper dosingMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private AccessoryMapper accessoryMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Override
    public UserOV<List<List<Category>>> getIndexCategoryList() {
        UserOV<List<List<Category>>> userOV = new UserOV<>();
        List<List<Category>> lists = new ArrayList<>();
        lists.add(categoryMapper.getIndexCategoryList());
        userOV.setData(lists).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<List<List<Category>>> getCategoryList() {
        UserOV<List<List<Category>>> userOV = new UserOV<>();
        List<List<Category>> lists = new ArrayList<>();
        lists.add(categoryMapper.getCategoryList());
        userOV.setData(lists).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<List<Dosing>> getAllDosing() {
        UserOV<List<Dosing>> userOV = new UserOV<>();
        userOV.setData(dosingMapper.getAllDosing()).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<Map<String,Object>> checkCommodityInCategory(Category category) throws TidcNulException {
        UserOV<Map<String,Object>> userOV = new UserOV<>();
        Map<String,Object> map = new HashMap<>();

        if(category.getId()!=null&&category.getCurPage()!=null&&category.getPageSize()!=null){
            category.setCurPage((category.getCurPage()-1)*category.getPageSize());
            int number = commodityMapper.inCategoryCommodityNumber(category.getId());
            int size =  number/category.getPageSize();
            if (number%category.getPageSize()>0) {
                size++;
            }
            map.put("size",size);
            map.put("number",number);
            map.put("list",commodityMapper.checkCommodityInCategory(category));
            userOV.setData(map).getMeta().setStatus(200);

        }else{
            throw new TidcNulException("参数不足以查询");
        }
        return userOV;
    }

    @Override
    public UserOV<Map<String, Object>> checkAdminCommodityInCategory(Category category) throws TidcNulException {
        UserOV<Map<String,Object>> userOV = new UserOV<>();
        Map<String,Object> map = new HashMap<>();
        if(category.getId()!=null&&category.getCurPage()!=null&&category.getPageSize()!=null){
            category.setCurPage((category.getCurPage()-1)*category.getPageSize());
            int number = commodityMapper.inCategoryCommodityNumber(category.getId());
            int size =  number/category.getPageSize();
            if (number%category.getPageSize()>0) {
                size++;
            }
            map.put("size",size);
            map.put("number",number);
            map.put("list",commodityMapper.checkAdminCommodityInCategory(category));
            userOV.setData(map).getMeta().setStatus(200);

        }else{
            throw new TidcNulException("参数不足以查询");
        }
        return userOV;
    }

    @Override
    public UserOV<List<Seckill>> getSeckillList() {
        UserOV<List<Seckill>> userOV = new UserOV<>();
        userOV.setData(seckillMapper.getSeckillList()).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<List<Cart>> getUserCartList(HttpServletRequest req) throws TidcNulException {
        User userInfo = redisUtil.getUserInfo(req);
        List<Cart> userCartList = cartMapper.getUserCartList(userInfo.getId());

        return null;
    }

    @Override
    public UserOV<Accessory> getAccessoryDosing(int id) {
        UserOV<Accessory> userOV = new UserOV<>();
        userOV.setData(accessoryMapper.getAccessoryDosing(id)).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<List<Accessory>> getAccessoryAll() {
        UserOV<List<Accessory>> userOV = new UserOV<>();
        userOV.setData(accessoryMapper.getAccessoryAll()).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<List<Coupon>> getCoupon() {
        UserOV<List<Coupon>> userOV = new UserOV<>();
        userOV.setData(couponMapper.getCouponAll()).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<List<User>> getUserAll() {
        UserOV<List<User>> userOV = new UserOV<>();
        userOV.setData(userMapper.getUserAll()).getMeta().setStatus(200);
        return userOV;
    }
}
